package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketCommentExample;
import com.cskaoyan.market.db.mapper.MarketCommentMapper;
import com.cskaoyan.market.service.MarketCommentService;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author lanch
 * @since 2023/05/08 21:25
 */
public class MarketCommentServiceIml implements MarketCommentService {
    //获取评论列表
/*    @Override
    public List<MarketComment> getCommentsList(String page, String limit, String sort, String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCommentMapper commentMapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample commentExample = new MarketCommentExample();
        commentExample.setOrderByClause(sort);
        //delete属性为true的才显示
        commentExample.or().andDeletedEqualTo(false);
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketComment> marketComments = commentMapper.selectByExample(commentExample);
        session.commit();
        session.close();
        return marketComments;
    }*/
    public List<MarketComment> getCommentsList(String page, String limit, String sort, String order,String userId,String valueId) {
        SqlSession session = MyBatisUtil.getSession();
        MarketCommentMapper commentMapper = session.getMapper(MarketCommentMapper.class);
        MarketCommentExample commentExample = new MarketCommentExample();
        //排序
        commentExample.orderBy(sort+" "+order);
        //delete属性为true的才显示
        MarketCommentExample.Criteria criteria = commentExample.or();
        criteria.andDeletedEqualTo(false);
        //如果查询会传入两个参数（至少一个）
        Integer userIdNum = null;
        Integer valueIdNum = null;

        if(!StringUtils.isEmpty(userId)){
            try{
                userIdNum = Integer.parseInt(userId);
            }catch (Exception e){
                //给userIdNum赋默认值为null
                userIdNum = null;
            }
        }
        if(!StringUtils.isEmpty(valueId)){
            try{
                valueIdNum = Integer.parseInt(valueId);
            }catch (Exception e){
                //给userIdNum赋默认值为null
                valueIdNum = null;
            }
        }
        if(!(userIdNum == null)){
            criteria.andUserIdEqualTo(userIdNum);
        }
        if(!(valueIdNum == null)){
            criteria.andValueIdEqualTo(valueIdNum);
        }

        //分页
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<MarketComment> marketComments = commentMapper.selectByExampleSelective(commentExample);
        session.commit();
        session.close();
        return marketComments;
    }
    //删除一条评论
    @Override
    public void deletComment(MarketComment reqBodyObject) {
        //删除一条评论实际上是将其delete属性置为true，商品列表刷新不显示它
        reqBodyObject.setDeleted(true);
        reqBodyObject.setUpdateTime(LocalDateTime.now());
        SqlSession session = MyBatisUtil.getSession();
        MarketCommentMapper commentMapper = session.getMapper(MarketCommentMapper.class);
        commentMapper.updateByPrimaryKeySelective(reqBodyObject);
        session.commit();
        session.close();
    }
}
